#coding=utf-8
from django.db import models
from exday import Exday
from stutils import float2dec

class Price(models.Model):
    exday = models.ForeignKey(Exday, unique=True)
    
    start = models.DecimalField( max_digits=8, decimal_places=2, null=True, blank=True)
    end = models.DecimalField( max_digits=8, decimal_places=2, null=True, blank=True)
    
    low = models.DecimalField( max_digits=8, decimal_places=2, null=True, blank=True)
    high = models.DecimalField( max_digits=8, decimal_places=2, null=True, blank=True)
    
    volume = models.IntegerField()
    amount = models.IntegerField()
    
    change = models.DecimalField( max_digits=6, decimal_places=2, null=True, blank=True )
    hsl = models.DecimalField( max_digits=6, decimal_places=2, null=True, blank=True )
    fluctuation = models.DecimalField( max_digits=6, decimal_places=2, null=True, blank=True )
    
    def get_change(self):
        '''涨跌'''  
        pre = self.exday.pre
        if not pre:return None
        
        pe = pre.price.end
        change = ( self.end - pe )/ pe * 100
        change = float2dec(change, 2)
        
        return change
    
    def get_hsl(self):
        '''换手率'''
        d = self.exday.date.isoformat()
        ts = self.exday.ts
        if not ts:
            #like 爱使股份(600652) in ipo day no 流通A股数量信息fromsina
            return None
        hsl = self.volume / float(ts) * 100
        hsl = float2dec(hsl, 2)
        
        return hsl
        
    def get_fluctuation(self):
        '''波动幅度'''
        f = ( self.high - self.low ) / self.end * 100
        f = float2dec(f, 2)
        return f
        
    def save(self):
        self.change = self.get_change()   
        self.hsl = self.get_hsl()
        self.fluctuation = self.get_fluctuation()
        super(Price, self).save()
        
    def __unicode__(self):
        jyr = unicode('交易日', 'u8')
        jgxx = unicode('的价格信息', 'u8')
        
        return "%s%s on %s(%s)"%(self.exday.share.name, jgxx, jyr, self.exday.date)
        
    class Admin:pass
    class Meta:
        app_label = 'shstock'
        
class Deal(models.Model):
    '''amount: 一笔交易的总金额'''
    exday = models.ForeignKey('Exday')
    
    time = models.TimeField()
    price = models.DecimalField( max_digits=6, decimal_places=2, null=True, blank=True)
    volume = models.IntegerField()
    amount = models.IntegerField()
    buyorsell = models.CharField(max_length=1)
    
    def save(self):
        self.amount = self.price * self.volume
        super(Deal, self).save()
        
    def __unicode__(self):
        cjmx = unicode('成交明细', 'u8')
        return "%s on (%s,%s) of %s"%(cjmx, self.exday.date.isoformat(), self.time.isoformat(), self.exday.share.name) 
 
    class Admin:pass
    class Meta:
        app_label = 'shstock'
        
class LivePrice(models.Model):
    
    exday = models.ForeignKey('Exday')
    
    time = models.TimeField()
    price = models.DecimalField( max_digits=6, decimal_places=2)
    change = models.DecimalField( max_digits=6, decimal_places=2)
    volume = models.IntegerField()
    amount = models.IntegerField()
    
    #买卖5档
    s1_price = models.DecimalField( max_digits=6, decimal_places=2)
    s1_volume = models.IntegerField()
    s2_price = models.DecimalField( max_digits=6, decimal_places=2)
    s2_volume = models.IntegerField()
    s3_price = models.DecimalField( max_digits=6, decimal_places=2)
    s3_volume = models.IntegerField()
    s4_price = models.DecimalField( max_digits=6, decimal_places=2)
    s4_volume = models.IntegerField()
    s5_price = models.DecimalField( max_digits=6, decimal_places=2)
    s5_volume = models.IntegerField()
    
    b1_price = models.DecimalField( max_digits=6, decimal_places=2)
    b1_volume = models.IntegerField()
    b2_price = models.DecimalField( max_digits=6, decimal_places=2)
    b2_volume = models.IntegerField()
    b3_price = models.DecimalField( max_digits=6, decimal_places=2)
    b3_volume = models.IntegerField()
    b4_price = models.DecimalField( max_digits=6, decimal_places=2)
    b4_volume = models.IntegerField()
    b5_price = models.DecimalField( max_digits=6, decimal_places=2)
    b5_volume = models.IntegerField()
    
    def __unicode__(self):
        ssjgxx = unicode('实时价格信息', 'u8')
        return "%s on (%s,%s) of %s"%(ssjgxx, self.exday.date.isoformat(), self.time.isoformat(), self.exday.share.name)
    
    class Admin:pass
    class Meta:
        app_label = 'shstock'
